﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Jidong
{
    public partial class 资产编码录入 : System.Web.UI.Page
    {
        protected string Notice = "";
        protected string Autorun = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string cmd = Request.QueryString["cmd"];
                string s = Request.QueryString["id"];
                if (s == null)
                {
                    return;
                }
                int id;
                if (!int.TryParse(s, out id))
                {
                    return;
                }
                this.FID.Text = id.ToString();
                if (cmd == null)
                {
                    return;
                }
                else if (cmd == "ed")
                {
                    this.Button_Save.Text = "保 存";
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = id.ToString();
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
                else if (cmd == "in")
                {
                    this.Button_Save.Text = "新增子类";

                    if (id == 0)
                    {
                        this.LV.Text = "0";
                    }
                    else if (id % 1000000 == 0)
                    {
                        this.LV.Text = "1";
                        this.ShangJi.Text = (id / 1000000).ToString("D2");
                    }
                    else if (id % 10000 == 0)
                    {
                        this.LV.Text = "2";
                        this.ShangJi.Text = (id / 10000).ToString("D4");
                    }
                    else if (id % 100 == 0)
                    {
                        this.LV.Text = "3";
                        this.ShangJi.Text = (id / 1000).ToString("D6");
                    }
                    else
                    {
                        this.Button_Save.Enabled = false;
                    }
                }
                else
                {
                    return;
                }
            }
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (!Convert.IsDBNull(e.Command.Parameters[1].Value))
            {
                int id = (int)e.Command.Parameters[0].Value;
                int fid = (int)e.Command.Parameters[1].Value;
                int lv = (int)e.Command.Parameters[2].Value;
                this.ShangJi.Text = id.ToString("D8");
                if (lv == 0)
                {
                    this.ShangJi.Text = (id / 1000000).ToString("D2");
                     }
                else if (lv == 1)
                {
                    this.ShangJi.Text = (id / 10000).ToString("D4");
                }
                else if (lv == 2)
                {
                    this.ShangJi.Text = (id / 100).ToString("D6");
                }
                else
                {
                    this.ShangJi.Text = id.ToString("D8");
                }


                this.BianMa.Text="1";
                this.BianMa.Visible = false;

                this.FID.Text = id.ToString();
                this.MingCheng.Text = (string)e.Command.Parameters[3].Value;
                if (!Convert.IsDBNull(e.Command.Parameters[4].Value))
                {
                    this.DanWei.Text = (string)e.Command.Parameters[4].Value;
                }
                if (!Convert.IsDBNull(e.Command.Parameters[5].Value))
                {
                    this.GuiGe.Text = (string)e.Command.Parameters[5].Value;
                }
                if (!Convert.IsDBNull(e.Command.Parameters[6].Value))
                {
                    this.ZheJiu.Text = ((byte)e.Command.Parameters[6].Value).ToString();
                }
                if (!Convert.IsDBNull(e.Command.Parameters[7].Value))
                {
                    this.CanZhi.Text = ((byte)e.Command.Parameters[7].Value).ToString();
                }
            }
            else
            {
                this.Notice = "物料分类查询失败";
            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {

            int id;
            this.Notice = "编码录入有误";
            if (!int.TryParse(this.BianMa.Text, out id))
            {
                return;
            }
            else if (id < 1 || id > 99)
            {
                return;
            }
            
            int zj=0;
            int.TryParse(this.ZheJiu.Text, out zj);
            this.ZheJiu.Text=zj.ToString();
            int cz = 0;
            int.TryParse(this.CanZhi.Text, out cz);
            this.CanZhi.Text = cz.ToString();

            this.Notice = "";
            if (this.Button_Save.Text == "保 存")
            {
                try
                {
                    if (this.SqlDataSource1.Update() > 0)
                    {
                        this.Notice = "保存成功";
                        this.Autorun = "parent.contents.document.getElementById('" + int.Parse(this.FID.Text).ToString("D8")
                            + "').parentNode.parentNode.firstChild.firstChild.firstChild.fireEvent('ondblclick')";
                    }
                }
                catch (Exception ex)
                {
                    this.Notice = ex.Message;
                    this.Notice = "保存失败";
                }
            }
            else
            {
                
                if (this.LV.Text == "0")
                {
                    id = int.Parse(this.FID.Text)+(id*1000000);
                }
                else if (this.LV.Text == "1")
                {
                    id = int.Parse(this.FID.Text) + (id * 10000);
                }
                else if (this.LV.Text == "2")
                {
                    id = int.Parse(this.FID.Text) + (id * 100);
                }
                else
                {
                    id = int.Parse(this.FID.Text) + id;
                }

                this.SqlDataSource1.InsertParameters["ID"].DefaultValue = id.ToString();
                try
                {
                    if (this.SqlDataSource1.Insert() > 0)
                    {
                        this.Notice = "新增子类成功";
                        this.Autorun = "parent.contents.document.getElementById('" + int.Parse(this.FID.Text).ToString("D8")
                            + "').firstChild.firstChild.firstChild.fireEvent('ondblclick')";
                    }
                }
                catch (Exception ex)
                {
                    this.Notice = ex.Message;
                    this.Notice = "新增子类失败，请检查名称是否已经填写，编码是否重复";
                }
            }
        }
    }
}
